<?php

/**
 * 充值及资金使用模块
 * ============================================================================
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/lib_goods.php');


/*------------------------------------------------------ */
//-- 充值及资金使用模块
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    /* 检查权限 */
    admin_priv('fund_use');

    /* 模板赋值 */
	$smarty->assign('ur_here',      "充值及资金使用列表");

    $list = get_list();
	// echo "<pre>";
	// print_r($list);
	// echo "<pre>";
	// die;
	
	/*支付方式*/
	$pay_lists = array();
	
	$payment_list = $GLOBALS['db']->getAll("SELECT pay_code, pay_name FROM " .$GLOBALS['ecs']->table('payment'). " WHERE enabled = '1' and is_online = '1' and pay_code !='balance' ");
	
	$touch_payment_list = $GLOBALS['db']->getAll("SELECT pay_code, pay_name FROM " .$GLOBALS['ecs']->table('touch_payment'). " WHERE enabled = '1' and is_online = '1' and pay_code !='balance'  ");
	
	if(!empty($payment_list)){
		
		if(!empty($touch_payment_list)){
			
			$pay_lists =  array_merge($payment_list,$touch_payment_list);
			
		}else{
			
			$pay_lists = $payment_list;
			
		}
		
	}else if(!empty($touch_payment_list)){
		
		$pay_lists = $touch_payment_list;
	}
	
	$pay_lists = array_unique_fb($pay_lists);
	

    $smarty->assign('pay_lists',    $pay_lists);

    $smarty->assign('list',    $list['list']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
    $smarty->assign('full_page',    1);
    $smarty->assign('sort_d_id', '<img src="images/sort_desc.gif">');

    /* 显示模板 */
    assign_query_info();
    $smarty->display('fund_use.htm');
}
/*------------------------------------------------------ */
//-- ajax返回列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    admin_priv('fund_use');   
    $list = get_list();
    $smarty->assign('list',    $list['list']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('fund_use.htm'), '', array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}



//二维数组去掉重复值,并保留键值 
function array_unique_fb($array2D){ 
	 foreach ($array2D as $k=>$v){ 
		 $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串  
		 $temp[$k]=$v; 
	 } 
	 $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组  
	 foreach ($temp as $k => $v){ 
		 $array=explode(',',$v); //再将拆开的数组重新组装  
		 //下面的索引根据自己的情况进行修改即可  
		 $temp2[$k]['pay_code'] =$array[0]; 
		 $temp2[$k]['pay_name'] =$array[1];
		 
	 }  
	return $temp2;
 } 



/**
 *  返回列表数据
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['user_id'] = empty($_REQUEST['user_id']) ? '' : trim($_REQUEST['user_id']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            //$filter['user_id'] = json_str_iconv($filter['user_id']);
        }
		
		/*充值时间段*/
		$filter['start_time'] = empty($_REQUEST['start_time']) ? '' : (strpos($_REQUEST['start_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_time']) : $_REQUEST['start_time']);
        $filter['end_time'] = empty($_REQUEST['end_time']) ? '' : (strpos($_REQUEST['end_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_time']) : $_REQUEST['end_time']);
		
		/*充值方式*/
		$filter['pay_code'] = empty($_REQUEST['pay_code']) ? '' : trim($_REQUEST['pay_code']);

		/*使用时间段*/
		$filter['start_change_time'] = empty($_REQUEST['start_change_time']) ? '' : (strpos($_REQUEST['start_change_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_change_time']) : $_REQUEST['start_change_time']);

        $filter['end_change_time'] = empty($_REQUEST['end_change_time']) ? '' : (strpos($_REQUEST['end_change_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_change_time']) : $_REQUEST['end_change_time']);
		
		
        $where = " WHERE a.user_money <> 0 ";
        if ($filter['user_id'])
        {
            $where .= " AND a.user_id ='".$filter['user_id']."'";
        }
		
		if($filter['pay_code']){
			
			$where .= " AND a.change_type = 0 AND a.pay_code ='".$filter['pay_code']."'";
			
			if ($filter['start_time'])
			{
				$where .= " AND a.change_time >= '$filter[start_time]'";
			}
			if ($filter['end_time'])
			{
				$where .= " AND a.change_time <= '$filter[end_time]'";
			}
			
		}else if(!empty($filter['start_time']) && !empty($filter['end_time'])){
			
			$where .= " AND a.change_type = 0 ";
			
			if ($filter['start_time'])
			{
				$where .= " AND a.change_time >= '$filter[start_time]'";
			}
			if ($filter['end_time'])
			{
				$where .= " AND a.change_time <= '$filter[end_time]'";
			}

			
		}else if(!empty($filter['start_change_time']) && !empty($filter['end_change_time'])){
			
			$where .= " AND a.change_type = 99 ";
			
			if ($filter['start_change_time'])
			{
				$where .= " AND a.change_time >= '$filter[start_change_time]'";
			}
			if ($filter['end_change_time'])
			{
				$where .= " AND a.change_time <= '$filter[end_change_time]'";
			}
			
		}else{
			
			$where .= " AND a.change_type in(0,99) ";
			
		}

		 /* 分页大小 */
        $filter['page'] = empty($_REQUEST['page']) || (intval($_REQUEST['page']) <= 0) ? 1 : intval($_REQUEST['page']);
		
		if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
        {
            $filter['page_size'] = intval($_REQUEST['page_size']);
        }
        elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
        {
            $filter['page_size'] = intval($_COOKIE['ECSCP']['page_size']);
        }
        else
        {
            $filter['page_size'] = 15;
        }
		

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) ". " FROM " . $GLOBALS['ecs']->table('account_log') . "AS a " . $where);

        $filter['page_count']     = $filter['record_count'] > 0 ? ceil($filter['record_count'] / $filter['page_size']) : 1;
		

        $sql = "SELECT a.*, u.user_name " . " FROM " . $GLOBALS['ecs']->table('account_log') . "AS a LEFT JOIN " . $GLOBALS['ecs']->table('users') . " AS u on a.user_id=u.user_id ". $where ." LIMIT " . ($filter['page'] - 1) * $filter['page_size'] . ",$filter[page_size]";
        
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
	
    $list = $GLOBALS['db']->getAll($sql);
	
    foreach ($list as $key => $item) {
		
        $list[$key]['change_time'] = date('Y-m-d h:m:s',$item['change_time']);

		$p_pay_code = $GLOBALS['db']->getOne("SELECT pay_name FROM " .$GLOBALS['ecs']->table('payment'). " WHERE pay_code = '".$item['pay_code']."' ");
			
		if(empty($p_pay_code)){
				
			$p_pay_code = $GLOBALS['db']->getOne("SELECT pay_name FROM " .$GLOBALS['ecs']->table('touch_payment'). " WHERE pay_code = '".$item['pay_code']."' ");
				
		}
		
		$list[$key]['pay_name'] = $p_pay_code;
		
    }
	
    $arr = array('list' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
		
    return $arr;
}


